<?php
namespace Common\Model;

/**
 * 后台管理-菜单分组模型
 *
 * @author Administrator
 *        
 */
class UserGroupModel extends CommModel {
	
	/**
	 * 获取后台菜单列表
	 * 
	 * @param string $auth 权限
	 * @return array
	 */
	public function menuList( $auth ) {
		$limit = array( 'begin' => 0, 'num' => 0 );
		$temp_map = array( 'pid' => 0, 'status' => 0, 'is_delete' => 0 );
		$tempList = $this->getList( $temp_map, "id,title,name,icon,inherit", $limit );
		for ( $i = 0, $size = count( $tempList ); $i < $size; $i ++ ) {
			$rows = $tempList[$i];
			$temp_map = array( 'pid' => $rows['id'], 'status' => 0, 'is_delete' => 0 );
			$child = $this->getList( $temp_map, "title,name,inherit", $limit );
			for ( $j = 0, $length = count( $child ); $j < $length; $j ++ ) {
				$temp = $child[$j];
				$child[$j]['url'] = empty( $temp['inherit'] ) ? "Admin/{$temp['name']}/index" : "Admin/{$rows['name']}/{$temp['name']}";				
				$child[$j]['rel'] = empty( $temp['inherit'] ) ? strtolower( $temp['name'] ) : strtolower( $rows['name'] );
				$temp_url = empty( $temp['inherit'] ) ? "{$temp['name']}/index" : "{$rows['name']}/{$temp['name']}";
				if ( strpos( $auth, ",{$temp_url}," ) === false && $_SESSION['userAuth']['id'] != 1) {
				    unset( $child[$j] );
				}
			}
			if ( count( $child ) == 0 ) {
				unset( $tempList[$i] );
			} else {
				$tempList[$i]['child'] = $child;
			}
		}
		return array_merge( $tempList );
	}
	/**
	 * 获取授权所用的节点
	 *
	 * @param string $auth 权限
	 * @return array
	 */
	public function authNodeList( $auth ) {
	    $limit = array( 'begin' => 0, 'num' => 0 );
	    $temp_condition = array( 'pid' => 0, 'is_delete' => 0, 'status' => 0 );
	    $nodeList = $this->getList( $temp_condition, "id,title,name,inherit", $limit );
	    for ( $i = 0, $size = count( $nodeList ); $i < $size; $i ++ ) {
	        $rows = $nodeList[$i];
	        $temp_condition = array( 'pid' => $rows['id'], 'is_delete' => 0, 'status' => 0 );
	        $child = $this->getList( $temp_condition, "id,title,name,inherit" );
	        // 判断是否拥有操作当前节点的权限
	        $nodeList[$i]['checked'] = strpos( $auth, ",{$rows['name']}," ) !== false ? ' checked="true"' : '';
	        for ( $j = 0, $length = count( $child ); $j < $length; $j ++ ) {
	            $temp = $child[$j];
	            if($temp['inherit'] == 1){
	               $child[$j]['checked'] = strpos( $auth, ",{$rows['name']}/{$temp['name']}," ) !== false ? ' checked="true"' : '';
	            }else{
	                $child[$j]['checked'] = strpos( $auth, ",{$temp['name']}/index," ) !== false ? ' checked="true"' : '';
	            }
	        }
	        $nodeList[$i]['child'] = empty( $child ) ? array() : $child;
	    }
	    return $nodeList;
	}
}